// routes/cards/findAllByGroup.js
const express = require('express');
const pool = require('../../config/pool'); // 使用连接池
const router = express.Router();
const isAdmin = require('../../middleware/isAdmin');

// 查询所有用户
router.get('/', isAdmin, (req, res) => {
    const query = 'SELECT * FROM users';
    try {
        pool.query(query, (err, results) => {
            if (err) {
                console.error('数据库错误:', err);
                return res.status(500).send({
                    code: 0,
                    message: '数据库错误',
                    success: false
                });
            }
            const list = results.map(item => {
                return {
                    id: item.id,
                    username: item.username,
                    phone_number: item.phone_number,
                    nickname: item.nickname,
                    avatar_url: item.avatar_url,
                    is_admin: item.is_admin,
                    created_at: item.created_at,
                    updated_at: item.updated_at
                }
            });
            res.status(200).json({
                code: 1,
                message: '查询成功',
                success: true,
                list
            });
        });
    } catch (error) {
        res.status(500).json({
            code: 0,
            message: error.message,
            success: false
        });
    }
});

module.exports = router;